// pages/laoLai/index.js
const apiFee = require('../../config/home.js');
const apiPay = require('../../config/api.js');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        searchValue: '',
        dataList: []
    },
    searchValueChange(e) {
        if (e.detail) {
            this.setData({
                searchValue: e.detail
            })
        }
    },
    clrarCeach() {
        this.setData({
            dataList: [],
            searchValue:null
        })
    },
    clickItemHander(e) {
        let id = e.currentTarget.dataset.id
        if (id && this.data.dataList) {
            let data = this.data.dataList.find(x => x.Id == id)
            data.deadBeatCheck = this.data.fee.deadBeatCheck
            console.log(data)
            if (data && data.Id) {
                let str = JSON.stringify(data)
                wx.navigateTo({
                    url: `/pages/laoLaiDetail/index?data=${str}`
                });
            }
        }
    },
    searchGo() {
        if (!this.data.searchValue ||this.data.searchValue == '') {
            wx.showToast({ title: '请输入老赖信息', icon: 'none' })
        } else {
            //开始付费查询
            this.toPay(parseFloat(this.data.fee.deadBeatCheck))
        }
    },
    toPay(fee) {
        let this_ = this;
        let openid = wx.getStorageSync('openid')
        wx.request({
            url: apiPay.wxPay,
            method: 'GET',
            data: {
                openid: openid,
                amount: fee,
                description: '搜老赖人员信息列表',
                vipGradeId: -1
            },
            success: (res) => {
                wx.hideLoading();
                if (res.data.code == 200) {
                    this_.setData({
                        param: res.data.data
                    })
                    this_.pay(JSON.parse(res.data.data))
                } else {
                    wx.showToast({
                        title: "网络异常！"
                    })
                }

            },
            fail: (res) => {

                wx.hideLoading();
            }
        })
    },
    //支付
    pay: function (param) {
        var that = this;
        wx.requestPayment({
            timeStamp: param.timeStamp,
            nonceStr: param.nonceStr,
            package: param.package,
            signType: param.signType,
            paySign: param.paySign,
            success: function (res) {
                that.getDataBaseList()
            },
            fail: function (res) { },
            complete: function (res) { }
        })
    },
    //获取老赖基本信息列表
    getDataBaseList() {
        wx.showLoading({ title: '正在加载' })
        let searchValue= this.data.searchValue
        let type= "0123456789".indexOf(searchValue.substring(0,1))
        console.log(type)
        let param = {}
        if(type == 1){
            param =  { searchNo: searchValue}
        }else{
            param =  { searchKey: searchValue}
        }
        wx.request({
            url: apiPay.deadBeatCheck,
            method: 'GET',
            data: param,
            success: (res) => {
                wx.hideLoading();
                if (res.data.code == 200) {
                    this.setData({
                        dataList: res.data.data || [],
                    })
                } else {
                    wx.showToast({
                        title: "网络异常！"
                    })
                }

            },
            fail: (res) => {

                wx.hideLoading();
            }
        })
       
        console.log('this.data', this.data.dataList)
        wx.hideLoading()
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getDictDataList()
    },
    getDictDataList() {
        wx.request({
          url: apiFee.dictDataListUrl,
          data: {
            type: 'miniprogram_fee'
          },
          method: 'GET',
          success: (res) => {
            console.log(res)
            if (res.data.code === 200) {
              let fee = {};
              let data = res.data.data
              data.forEach(element => {
                let key = element.dictLabel
                let value = element.dictValue
                if (key == "deadBeatCheck") {
                  fee.deadBeatCheck = value
                } 
              });
              this.setData({
                fee
              })
            }
          }
        });
      },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})